package com.fjwt.gz.proxy.ctrl.award.awardmch;


import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fjwt.gz.core.model.ApiRes;
import com.fjwt.gz.db.dto.AwardMchDTO;
import com.fjwt.gz.db.vo.AwardMchVO;
import com.fjwt.gz.proxy.ctrl.CommonCtrl;
import com.fjwt.gz.service.impl.AwardMchService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/***
 * 奖品可核销商户中间表 Controller
 *
 */
@RestController
@Slf4j
@RequestMapping("/api/award/mch")
public class AwardMchController extends CommonCtrl {


    @Autowired
    private AwardMchService awardMchService;

    /**
     * 获取可核销商户列表
     *
     * @return
     */
    @PostMapping("/getMchDestroyList")
    public ApiRes getMchDestroyList() {
        AwardMchDTO awardMchDTO = getObject(AwardMchDTO.class);
        log.info("机构端获取可核销商户列表，请求地址：/api/award/mch/getMchDestroyList，请求参数：{}", JSONUtil.toJsonStr(awardMchDTO));
        IPage<AwardMchVO> page = awardMchService.getMchDestroyList(getIPage(true), awardMchDTO);
        log.info("机构端获取可核销商户列表，请求地址：/api/award/mch/getMchDestroyList，返回结果：{}", JSONUtil.toJsonStr(page));
        return ApiRes.page(page);
    }


    /**
     * 批量导入可核销商户
     *
     * @return
     */
    @PostMapping("/importMchDestroyFile")
    public ApiRes importMchDestroyFile() {
        AwardMchDTO awardMchDTO = getObject(AwardMchDTO.class);
        log.info("机构端批量导入可核销商户，请求地址：/api/award/mch/importMchDestroyFile，请求参数：{}", JSONUtil.toJsonStr(awardMchDTO));
        JSONObject jsonObject = awardMchService.importBatchMchDestroyByFile(awardMchDTO);
        return ApiRes.ok(jsonObject);
    }


    /**
     * 新增单个可核销商户
     *
     * @return
     */
    @PostMapping("/addAwardMch")
    public ApiRes addAwardMch() {
        AwardMchDTO awardMchDTO = getObject(AwardMchDTO.class);
        log.info("机构端新增单个可核销商户，请求地址：/api/award/mch/addAwardMch，请求参数：{}", JSONUtil.toJsonStr(awardMchDTO));
        awardMchService.addOneMchDestroy(awardMchDTO);
        return ApiRes.ok();
    }

    /**
     * 更新可核销商户的渠道码是否可见
     * @return
     */
    @PostMapping("/updateMchDestroyChannelDisplay")
    public ApiRes updateMchDestroyChannelDisplay() {
        AwardMchDTO awardMchDTO = getObject(AwardMchDTO.class);
        log.info("机构端更新可核销商户的渠道码是否可见，请求地址：/api/award/mch/updateMchDestroyChannelDisplay，请求参数：{}", JSONUtil.toJsonStr(awardMchDTO));
        awardMchService.updateMchDestroyChannelDisplay(awardMchDTO);
        return ApiRes.ok();
    }

    /**
     * 删除可核销商户
     */
    @PostMapping("/deleteMchDestroy")
    public ApiRes deleteMchDestroy() {
        Long awardMchId = getValLong("awardMchId");
        if (null == awardMchId) {
            return ApiRes.customFail("请求参数缺失！");
        }
        log.info("机构端删除可核销商户，请求地址：/api/award/mch/deleteMchDestroy，请求参数：{}", awardMchId);
        awardMchService.removeById(awardMchId);
        return ApiRes.ok();
    }

    /**
     * 删除所有可核销商户
     *
     * @return
     */
    @PostMapping("/removeAllMchDestroy")
    public ApiRes removeAllMchDestroy() {
        Long awardId = getValLong("awardId");
        Integer awardType = getValInteger("awardType");
        log.info("机构端删除所有可核销商户，请求地址：/api/award/mch/removeAllMchDestroy，请求参数：awardId:{}，awardType：{}", awardId, awardType);
        awardMchService.removeAllMchDestroyByAwardId(awardId, awardType);
        return ApiRes.ok();
    }

}
